<template>
  <div class="login-con">
    <div class="logo">
      <img src="../../assets/images/logo.png" alt="" />
    </div>
    <div class="form-login">
      <!-- 表单logo -->
      <div class="logo-title">
        <img src="../../assets/images/login_title.png" alt="" />
      </div>
      <!-- 表单 -->
      <div class="form-box">
        <el-form
          :model="loginForm"
          :rules="rulesForm"
          ref="loginForm"
          class="demo-loginForm"
        >
          <el-form-item prop="username">
            <el-input
              prefix-icon="el-icon-user"
              v-model="loginForm.username"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              prefix-icon="el-icon-key"
              v-model="loginForm.password"
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="subBtn" @click="submitBtn()"
              >登录</el-button
            >
            <el-button
              type="warning"
              class="subBtn"
              @click="$router.push('/reg')"
              >去注册</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// 导入接口请求
import { loginForm } from '../../api/user'
export default {
  name: '',
  data () {
    return {
      loginForm: {
        username: 'sbxx',
        password: '123456'
      },
      rulesForm: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {},
  computed: {},
  methods: {
    submitBtn () {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          // 接口请求使用
          const { data } = await loginForm(this.loginForm)
          console.log(data)
          if (data.code === 0) {
            this.$message({
              message: '登陆成功',
              type: 'success'
            })
            localStorage.setItem('token', data.token)
            this.$router.push('/')
          } else {
            this.$message({
              message: '登陆失败',
              type: 'error'
            })
            return false
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang='less'  scoped>
.login-con {
  width: 100vw;
  height: 100vh;
  background-color: rgb(1, 133, 116);
  background: url('../../assets/images/login_bg.jpg') no-repeat;
  background-size: cover;
  position: relative;
  .logo {
    width: 1200px;
    margin: 0 auto;
  }
  .form-login {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-sizing: border-box;
    padding: 20px 30px;

    .logo-title {
      text-align: center;
      margin-bottom: 15px;
    }

    .form-box {
      .subBtn {
        width: 45%;
        margin-left: 15px;
      }
    }
  }
}
</style>
